<?php

function convert2Dto1D($array2D) {
    $result = array();
    foreach ($array2D as $item) {
        $result[] = $item[0];
    }
    return $result;
}

function getAvailableResourcesCount($slot_date, $resource_type_id) {
    $max_available_resources = 0;
    //$resource_count = ResourceDB::getResourceCount($resource_type_id);
    $slots_inventory = SlotDB::getAllSlots();

    if (!is_null($slots_inventory)) {
        $slots_inventory->rewind();
        while ($slots_inventory->valid()) {
            $current_slot = $slots_inventory->current();
            $reserved_resources_ids = ReservationDB::getReservedResourceIDs($slot_date, $current_slot->getSlot_id(), $resource_type_id);
            $all_resources_ids = ResourceDB::getAllResourceIDs($resource_type_id);
            $array1 = convert2Dto1D($all_resources_ids);
            $array2 = convert2Dto1D($reserved_resources_ids);
            $array3 = array_diff($array1, $array2);
            $array3 = array_values($array3);
            $available_resources = count($array3);
            if ($available_resources > $max_available_resources) {
                $max_available_resources = $available_resources;
            }
            $slots_inventory->next();
        }
    }
    return $max_available_resources;
}

function isSlotAvailable($slot_id, $slot_date, $resource_type_id) {
    $reserved_resources_ids = ReservationDB::getReservedResourceIDs($slot_date, $slot_id, $resource_type_id);
    $all_resources_ids = ResourceDB::getAllResourceIDs($resource_type_id);
    $array1 = convert2Dto1D($all_resources_ids);
    $array2 = convert2Dto1D($reserved_resources_ids);
    $array3 = array_diff($array1, $array2);
    $array3 = array_values($array3);
    $available_resources = count($array3);
    if ($available_resources > 0) {
        return true;
    } else {
        return false;
    }
}

function getResourceUtilization($resource_type_id, $year, $month, $week)
{
    $slots_count = SlotDB::getSlotsCount();
    $total_hours_per_week = $slots_count * 5;//asumming resources are not reserved during the weekends
    $date = $year . '-'  . $month . '-' . '1';
    $resource_type_name = ResourceTypeDB::getResourceTypeNameByID($resource_type_id);
    $to_date = date('Y-m-d', strtotime($week, strtotime($date)));
    $from_date = date('Y-m-d', strtotime('-1 week', strtotime($to_date)));
    $reserved_hours_per_week = ReservationDB::getReservedSlotCountForResourceType($from_date, $to_date, $resource_type_id);
    $ratio = $reserved_hours_per_week / $total_hours_per_week;
    $output = 'Resource utilization for ' . 
                $resource_type_name . ' from the period from ' . $from_date . ' to ' . $to_date . 
                ' is '; 
    
    if($ratio > 0.7)
    {
        return $output . 'high.';
    }
    elseif($ratio > 0.36)
    {
        return $output . 'medium.';
    }
    else
    {
        return $output . 'low.';
    }
        
}

?>
